]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
autorunManager: Promisify ContentTypeDiscoverer
authorFlorian Müllner <fmuellner@gnome.org>
Thu, 23 Jun 2022 15:02:55 +0000 (17:02 +0200)
committerMarge Bot <marge-bot@gnome.org>
Mon, 1 Aug 2022 18:51:14 +0000 (18:51 +0000)
Thanks to recent gjs changes - namely the ability to promisify
interface prototypes and promise-based D-Bus wrappers - we can
modernize the ContentTypeDiscoverer API to use an async function
instead of a callback.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2344>

js/ui/components/autorunManager.js

index c4a0dd2ec7c836d88c8cc75f21120b977fa29e4a..0e3fa9385beb506700cc528f4ea2756da2e812e2 100644 (file)
@@ -7,6 +7,8 @@ const GnomeSession = imports.misc.gnomeSession;
 const Main = imports.ui.main;
 const MessageTray = imports.ui.messageTray;
 
+Gio._promisify(Gio.Mount.prototype, 'guess_content_type');
+
 const { loadInterfaceXML } = imports.misc.fileUtils;
 
 // GSettings keys
@@ -81,64 +83,45 @@ function HotplugSniffer() {
 }
 
 var ContentTypeDiscoverer = class {
-    constructor(callback) {
-        this._callback = callback;
+    constructor() {
         this._settings = new Gio.Settings({ schema_id: SETTINGS_SCHEMA });
     }
 
-    guessContentTypes(mount) {
+    async guessContentTypes(mount) {
         let autorunEnabled = !this._settings.get_boolean(SETTING_DISABLE_AUTORUN);
         let shouldScan = autorunEnabled && !isMountNonLocal(mount);
 
-        if (shouldScan) {
-            // guess mount's content types using GIO
-            mount.guess_content_type(false, null,
-                                     this._onContentTypeGuessed.bind(this));
-        } else {
-            this._emitCallback(mount, []);
-        }
-    }
-
-    _onContentTypeGuessed(mount, res) {
         let contentTypes = [];
-
-        try {
-            contentTypes = mount.guess_content_type_finish(res);
-        } catch (e) {
-            log(`Unable to guess content types on added mount ${mount.get_name()}: ${e}`);
-        }
-
-        if (contentTypes.length) {
-            this._emitCallback(mount, contentTypes);
-        } else {
-            let root = mount.get_root();
-
-            let hotplugSniffer = new HotplugSniffer();
-            hotplugSniffer.SniffURIRemote(root.get_uri(),
-                result => {
-                    [contentTypes] = result;
-                    this._emitCallback(mount, contentTypes);
-                });
+        if (shouldScan) {
+            try {
+                contentTypes = await mount.guess_content_type(false, null);
+            } catch (e) {
+                log(`Unable to guess content types on added mount ${mount.get_name()}: ${e}`);
+            }
+
+            if (contentTypes.length === 0) {
+                const root = mount.get_root();
+                const hotplugSniffer = new HotplugSniffer();
+                [contentTypes] = hotplugSniffer.SniffURIAsync(root.get_uri());
+            }
         }
-    }
 
-    _emitCallback(mount, contentTypes = []) {
         // we're not interested in win32 software content types here
         contentTypes = contentTypes.filter(
             type => type !== 'x-content/win32-software');
 
-        let apps = [];
+        const apps = [];
         contentTypes.forEach(type => {
-            let app = Gio.app_info_get_default_for_type(type, false);
+            const app = Gio.app_info_get_default_for_type(type, false);
 
             if (app)
                 apps.push(app);
         });
 
-        if (apps.length == 0)
+        if (apps.length === 0)
             apps.push(Gio.app_info_get_default_for_type('inode/directory', false));
 
-        this._callback(mount, apps, contentTypes);
+        return [apps, contentTypes];
     }
 };
 
@@ -160,16 +143,15 @@ var AutorunManager = class {
         this._volumeMonitor.disconnectObject(this);
     }
 
-    _onMountAdded(monitor, mount) {
+    async _onMountAdded(monitor, mount) {
         // don't do anything if our session is not the currently
         // active one
         if (!this._session.SessionIsActive)
             return;
 
-        let discoverer = new ContentTypeDiscoverer((m, apps, contentTypes) => {
-            this._dispatcher.addMount(mount, apps, contentTypes);
-        });
-        discoverer.guessContentTypes(mount);
+        const discoverer = new ContentTypeDiscoverer();
+        const [apps, contentTypes] = await discoverer.guessContentTypes(mount);
+        this._dispatcher.addMount(mount, apps, contentTypes);
     }
 
     _onMountRemoved(monitor, mount) {